package com.abctime.demo.eval_engine;

import android.content.Context;
import android.os.Environment;

import com.abctime.api.VoiceEvalEngine;
import com.tal.ailab.speech.TALVoiceEvalEngine;
import com.tal.ailab.speech.entity.SDKParam;
import com.tal.ailab.speech.entity.ServerTypeEnum;
import com.tal.ailab.speech.entity.VoiceEvalTypeEnum;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

/**
 * description: 语音工具引擎
 * author: KisenHuang
 * email: KisenHuang@163.com
 * time: 2018/7/5 下午3:59
 */

public class ABCVoiceEvalEngine implements VoiceEvalEngine {

    private String voiceEvalPath;
    private TALVoiceEvalEngine mEvalEngine;
    private OnVoiceEvalResultListener mOnVoiceEvalResultListener;
    private OnRecordListener mOnRecordListener;
    private TALVoiceEvalEngine.ResultListener mResultListener;

    @Override
    public void newEngine(Context context) {
        createRecPath(context.getApplicationContext());
        //将此处替换成自己的appId和key
        SDKParam param = new SDKParam("t123", "8ebb9ddf76fa4b11bde4671387b39897");
//        SDKParam param = new SDKParam("a342", "c11163aa6c834a028da4a4b30955be23");//趣配音新key,,更换于2018-10-10
        mEvalEngine = new TALVoiceEvalEngine(context.getApplicationContext(), param, 1, 4);
        mEvalEngine.setOpenVad(false);
        //设置评测引擎的配置、需要在newEngine方法前构造好
        mEvalEngine.setVoiceEvalType(VoiceEvalTypeEnum.EVAL_TYPE_LOCAL_SENT);
        mEvalEngine.setServerType(ServerTypeEnum.SERVER_TYPE_NATIVE);
        mEvalEngine.setFrontVadTime(2000);
        mEvalEngine.setBackVadTime(2000);
        mEvalEngine.setRealtimeEval(false);
        createResultListener();
        mEvalEngine.setListener(mResultListener);
        //构造一个新的引擎
        mEvalEngine.newEngine();
    }

    private void createResultListener() {
        mResultListener = new TALVoiceEvalEngine.ResultListener() {
            @Override
            public void onBegin() {
            }

            @Override
            public void onResult(String result) {
                if (mOnVoiceEvalResultListener != null) {
                    try {
                        JSONObject object = new JSONObject(result);
                        JSONObject resultStr = object.optJSONObject("result");
                        String overall = resultStr.optString("overall");
                        String recordId = object.optString("tokenId");

                        final String wavFilePath = voiceEvalPath + "/" + recordId + ".wav";
                        if (Integer.valueOf(overall) <= 0) {
                            overall = "1";
                        }
                        mOnVoiceEvalResultListener.onVoiceEvalResult(wavFilePath, overall);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        if (mOnRecordListener != null)
                            mOnRecordListener.onRecordStop(e);
                    }
                }
            }

            @Override
            public void onRealTimeEval(String s) {
            }

            @Override
            public void onEnd(int i, String s) {
                if (i != 0 && mOnRecordListener != null) {
                    mOnRecordListener.onRecordStop(new Throwable(s));
                }
            }

            @Override
            public void onUpdateVolume(int i) {
            }

            @Override
            public void onFrontVadTimeOut() {
            }

            @Override
            public void onBackVadTimeOut() {
            }

            @Override
            public void onRecordingBuffer(byte[] bytes, int i) {
            }

            @Override
            public void onRecordLengthOut() {
            }

            @Override
            public void onReady() {
            }

            @Override
            public void onPlayCompeleted() {
            }

            @Override
            public void onRecordStop() {
                if (mOnRecordListener != null)
                    mOnRecordListener.onRecordStop(null);
            }
        };
    }

    private void createRecPath(Context context) {
        if (context != null) {
            File externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_DCIM);
            if (externalFilesDir != null) {
                voiceEvalPath = externalFilesDir.getAbsolutePath();
            }
        }
    }

    @Override
    public void setOnVoiceEvalStopListener(OnVoiceEvalResultListener listener) {
        mOnVoiceEvalResultListener = listener;
    }

    @Override
    public void setOnRecordStopListener(OnRecordListener listener) {
        mOnRecordListener = listener;
    }

    @Override
    public boolean isAvailable() {
        return mEvalEngine != null && mEvalEngine.isAvailable();
    }

    @Override
    public boolean isRecording() {
        return mEvalEngine != null && mEvalEngine.isRecording();
    }

    @Override
    public void startRecordEval(String text) {
        if (mEvalEngine != null)
            mEvalEngine.startRecordEval(text, voiceEvalPath);
    }

    @Override
    public void stopRecord() {
        if (mEvalEngine != null)
            mEvalEngine.stopRecord();
    }

    @Override
    public void cancelRecord() {
        if (mEvalEngine != null)
            mEvalEngine.cancel();
    }

    @Override
    public void deleteRecord() {
        if (mEvalEngine != null) {
            mEvalEngine.delete();
            mEvalEngine = null;
        }
        mOnVoiceEvalResultListener = null;
        mOnRecordListener = null;
        mResultListener = null;
        voiceEvalPath = null;
    }

    @Override
    public boolean isNetworking() {
        return false;
    }

    @Override
    public int leastRecordMillisecond() {
        return 1000;
    }
}
